/**
 * @author chenyz
 */
control = {
	untitledIndex : 1,
	init : function() {
	},
	addEmptyTab : function() {
		domtab.addNewTab("untitled" + control.untitledIndex, "");
		control.untitledIndex++;
	},
	openFileFromLocal : function(fileList) {
		var filename = fileList[0].name;
		domtab.addNewTab(filename, "", "FROMLOCAL");
		filefolder.readFile(fileList[0]);
	},
	saveFileToLocal : function() {
		var id = domtab.activeFile;
		var saveCodeMirror = domtab.getCodeMirror(id);
		var saveTitle = domtab.getTitle(id);
		if (saveTitle.getAttribute("status") == "MODIFIED") {
			saveCodeMirror.save();
			var bb = new BlobBuilder();
			bb.append(saveCodeMirror.getValue());
			var type = filefolder.getMIMEType(id);
			saveAs(bb.getBlob(type + ";charset=utf-8"), id);

			saveTitle.setAttribute("status", "UNCHANGED");
			saveTitle.innerHTML = saveTitle.innerHTML.split("*")[1];
		}
	},
	closeTab : function() {
		var id = domtab.activeFile;
		domtab.closeTabHelper(id);
	},
	closeAll : function() {
		var ids = domtab.getAllOpenedTabId();
		for ( var i = 0; i < ids.length; i++) {
			domtab.closeTabHelper(ids[i]);
		}
	},
	closeOthers : function() {
		var ids = domtab.getAllOpenedTabId();
		var id = domtab.activeFile;
		for ( var i = 0; i < ids.length; i++) {
			if (ids[i] != id)
				domtab.closeTabHelper(ids[i]);
		}
	},
	openFileFromServer : function(filename, filecontent, filetype, filepath) {
	    if(!filecontent) filecontent='';
		domtab.addNewTab(filename, filecontent, filetype, 'souce from server', filepath);
	},
	closeFile : function(e) {
		domtab.closeTab(e);
	},
	increaseFontSize : function() {
		var ids = domtab.getAllOpenedTabId();
		for ( var i = 0; i < ids.length; i++) {
			var content = domtab.getContent(ids[i]);
			content.style.fontSize = "50pt";
			var cm = domtab.getCodeMirror(ids[i]);
			cm.refresh();
		}
	},
	switchGutter : function() {
		var ids = domtab.getAllOpenedTabId();
		for ( var i = 0; i < ids.length; i++) {
			var cm = domtab.getCodeMirror(ids[i]);
			var status = cm.getOption("lineNumbers");
			if (status == true) {
				cmeditor.switchGutter(cm, false);
				pf.addCMPF("lineNumbers", "false");
			} else {
				cmeditor.switchGutter(cm, true);
				pf.addCMPF("lineNumbers", "true");
			}
		}

	},
	switchStatusBar : function() {
		var statusBar = document.getElementById("statusbar");
		if (statusBar) {
			if (statusBar.style.display == "none") {
				statusBar.style.display = "block";
				pf.addSBPF("block");
			} else {
				statusBar.style.display = "none";
				pf.addSBPF("none");
			}
		}
	},
	changeTheme : function(themename) {
		var ids = domtab.getAllOpenedTabId();
		for ( var i = 0; i < ids.length; i++) {
			var cm = domtab.getCodeMirror(ids[i]);
			cmeditor.changeTheme(cm, themename);
		}
		pf.addCMPF("theme", themename);
	},
	changeSyntax : function(newSyntax) {
		var ids = domtab.getAllOpenedTabId();
		for ( var i = 0; i < ids.length; i++) {
			var cm = domtab.getCodeMirror(ids[i]);
			cm.setOption("mode", newSyntax);
		}
	},
	switchWrapLine : function() {
		var ids = domtab.getAllOpenedTabId();
		for ( var i = 0; i < ids.length; i++) {
			var cm = domtab.getCodeMirror(ids[i]);
			var status = cm.getOption("lineWrapping");
			if (status == true) {
				cm.setOption("lineWrapping", false);
			} else {
				cm.setOption("lineWrapping", true);
			}
		}
	},
	autoformat : function() {
		var cm = domtab.getCodeMirror(domtab.activeFile);
		cm.autoFormatRange(cm.getCursor(true), cm.getCursor(false));
	},
	commentSelection : function(isComment) {
		var cm = domtab.getCodeMirror(domtab.activeFile);
		cm.commentRange(isComment, cm.getCursor(true), cm.getCursor(false));
	},
	undo : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.undo(activeCodeMirror);
	},
	redo : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.redo(activeCodeMirror);
	},
	selectAll : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.selectAll(activeCodeMirror);
	},
	killLine : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.killLine(activeCodeMirror);
	},
	deleteLine : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.deleteLine(activeCodeMirror);
	},
	goDocStart : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goDocStart(activeCodeMirror);
	},
	goDocEnd : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goDocEnd(activeCodeMirror);
	},
	goLineStart : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goLineStart(activeCodeMirror);
	},
	goLineStartSmart : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goLineStartSmart(activeCodeMirror);
	},
	goLineEnd : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goLineEnd(activeCodeMirror);
	},
	goLineUp : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goLineUp(activeCodeMirror);
	},
	goLineDown : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goLineDown(activeCodeMirror);
	},
	goPageUp : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goPageUp(activeCodeMirror);
	},
	goPageDown : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goPageDown(activeCodeMirror);
	},
	goCharLeft : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goCharLeft(activeCodeMirror);
	},
	goPageUp : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goPageUp(activeCodeMirror);
	},
	goPageDown : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goPageDown(activeCodeMirror);
	},
	goCharLeft : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goCharLeft(activeCodeMirror);
	},
	goCharRight : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goCharRight(activeCodeMirror);
	},
	goColumnLeft : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goColumnLeft(activeCodeMirror);
	},
	goColumnRight : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goColumnRight(activeCodeMirror);
	},
	goWordLeft : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goWordLeft(activeCodeMirror);
	},
	goWordRight : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goWordRight(activeCodeMirror);
	},
	delCharLeft : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.delCharLeft(activeCodeMirror);
	},
	delCharRight : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.delCharRight(activeCodeMirror);
	},
	delWordLeft : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.delWordLeft(activeCodeMirror);
	},
	delWordRight : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.delWordRight(activeCodeMirror);
	},
	indentAuto : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.indentAuto(activeCodeMirror);
	},
	indentMore : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.indentMore(activeCodeMirror);
	},
	indentLess : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.indentLess(activeCodeMirror);
	},
	insertTab : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.insertTab(activeCodeMirror);
	},
	defaultTab : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.defaultTab(activeCodeMirror);
	},
	transposeChars : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.transposeChars(activeCodeMirror);
	},
	newlineAndIndent : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.newlineAndIndent(activeCodeMirror);
	},
	toggleOverwrite : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.toggleOverwrite(activeCodeMirror);
	},
	moveLineUp : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.moveLine(activeCodeMirror);
	},
	moveLineDown : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.moveLineDown(activeCodeMirror);
	},
	// copyLineUp : function() {
	// var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
	// CodeMirror.commands.copyLineUp(activeCodeMirror);
	// },
	copyLineDown : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.copyLineDown(activeCodeMirror);
	},
	removeToLineStart : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.removeToLineStart(activeCodeMirror);
	},
	uppperCase : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.uppperCase(activeCodeMirror);
	},
	lowerCase : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.lowerCase(activeCodeMirror);
	},
	selectWordRight : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.selectWordRight(activeCodeMirror);
	},
	selectWordLeft : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.selectWordLeft(activeCodeMirror);
	},
	selectLineEnd : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.selectLineEnd(activeCodeMirror);
	},
	selectLineStart : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.selectLineStart(activeCodeMirror);
	},
	selectDocumentEnd : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.selectDocumentEnd(activeCodeMirror);
	},
	selectDocumentStart : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.selectDocumentStart(activeCodeMirror);
	},
	goToLine : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goToLine(activeCodeMirror);
	},
	goToMatchingBrace : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.goToMatchingBrace(activeCodeMirror);
	},
	find : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.find(activeCodeMirror);
	},
	findNext : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.findNext(activeCodeMirror);
	},
	findPrev : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.findPrev(activeCodeMirror);
	},
	replace : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.replace(activeCodeMirror);
	},
	killLine : function() {
		var activeCodeMirror = domtab.getCodeMirror(domtab.activeFile);
		CodeMirror.commands.killLine(activeCodeMirror);
	},
	runBeautify : function() {
		var cm = domtab.getCodeMirror(domtab.activeFile);
		if (!cm)
			return;
		var selection = cm.getSelection();
		if (!selection || selection == "") {
			var sourceCode = cm.getValue();
			var result = beautifyRun(sourceCode);
			cm.setValue(result);
			cm.save();
		} else {
			var result = beautifyRun(selection);
			cm.replaceSelection(result);
			cm.save();
		}
	},
	runJshint : function() {
		var cm = domtab.getCodeMirror(domtab.activeFile);
		if (!cm)
			return;
		var selection = cm.getSelection();
		if (!selection || selection == "") {
			var sourceCode = cm.getValue();
			jshintRun(sourceCode);
		} else {
			jshintRun(selection);
		}
	},
        foldS:function(){
	       var cm = domtab.getCodeMirror(domtab.activeFile);
	       cm.getOption("onGutterClick")(cm,CodeMirror.commands.foldS(cm));
	},
        foldSA:function(){
	       var cm=domtab.getCodeMirror(domtab.activeFile);
	       cm.setCursor({line:cm.lineCount(),ch:0});
	       var line=CodeMirror.commands.foldS(cm);
	       while(line>=0)
	    {
	       cm.getOption("onGutterClick")(cm,line);
	       line--;
	       if(line<0)break;
	       cm.setCursor({line:line,ch:0});    
	    }

	},
	autoComplete : function() {
	    //alert("auto-complete");
	},
	save : function() {
	    control.save_helper(domtab.activeFile);
	},
	save_helper : function(id) {
	  var cm = domtab.getCodeMirror(id);
	  if(cm)
	    content = cm.getValue();
	  else content = '';
	  var titleDOM = domtab.getTitle(id);
	  
	  var exist = titleDOM.getAttribute("from_existing_server");
	  //alert(exist);
	  //console.log(exist);
	  if( exist == "true" ) {
	    createSaveFileDiv(content, id);
	    return;
	    // info = control.askInfo();
	    // filename = info[0];
	    // filepath = info[1];
	  }
	  else {
	    var filename = titleDOM.getAttribute("title");
	    var filepath = titleDOM.getAttribute("path");
	    //console.log(titleDOM);
	    //console.log(filename);
	    //console.log(filepath);
	    //console.log(content);
	  }
	  //alert(filename);
	  //alert(content);
	  //alert(filepath);
	  saveFile(filepath, content, filename);
	  var titleHTML = titleDOM.innerHTML;
	  titleDOM.innerHTML = titleHTML.split("*")[1];
	  //console.log(titleDOM.innerHTML);
	  titleDOM.setAttribute("status", "UNCHANGED");
	  fileTreeRefresh();
	},
	  
	
	displayNewTab : function(filename, content, filepath) {
     var type = filename.split(".")[1];
     if (type == "css" ) filetype  = "text/css";
     else if (type == "html" || type == "htm") filetype = "text/html";
     else if (type == "js") filetype = "text/javascript";
     else filetype  = "unknown";
     
     //console.log(filename.split("."));
     
     var tabs = domtab.getAllOpenedTabId();
     if(!tabs) {
         control.openFileFromServer(filename, content, filetype, filepath);
         return;
     }
     for (var i=0; i<tabs.length; i++) {
        if (domtab.getTitle(tabs[i]).title == filename) {
            // jump to this page
            domtab.showContent_helper(tabs[i]);
            return;
        }
     }
     control.openFileFromServer(filename, content, filetype, filepath);
	},
	askInfo : function() {
	  return ["domtab.js", "/home/"];
	},
};
